<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\home\controller;
use app\home\model\MemberModel;
use cmf\controller\UserBaseController;
use think\Db;

class JiaojieController extends UserBaseController
{
    public function index(){
        $uid= cmf_get_current_user_id();
        $uinfo=get_operator_data($uid);
        $this->assign('uinfo',$uinfo);
        return $this->fetch();
    }
    public function supply(){             
        $payType=array('1'=>'会员卡支付','2'=>'现金支付','3'=>'积分支付','4'=>'微信支付','5'=>'支付宝','6'=>'外卖支付');
        $this->assign('payType',$payType);
        return $this->fetch();
    }
    public function supplyPost(){
        $param = $this->request->param();
        $uinfo= cmf_get_current_user();
        $param['storeid']=$uinfo['store_id'];
        $param['operatorid']=$uinfo['id'];
        $param['ctime']=time();
        $re=Db::name('Supply')->insert($param);
        if($re){
            $this->success("补充成功！",'Home/jiaojie/supplyList');
        }else{
            $this->error("补充失败！",'Home/jiaojie/supply');
        }
    }
    public function supplylist()
    {
        /**搜索条件**/
        $uinfo=cmf_get_current_user();
        $request = input('request.');
        $where='';
        //判断店长还是收银员
        $checkUser=get_dianzhang($uinfo['id'],$uinfo['store_id']);
        if($checkUser){
            $where='storeid='.$uinfo['store_id'];
        }else{
            $where='operatorid='.$uinfo['id'];
        }
        if(!empty($request['start_time'])){
            $where .= ' and DATE( FROM_UNIXTIME( ctime ) ) >= "'.$request['start_time'].'"';
        }else{
            $where .= ' and DATE( FROM_UNIXTIME( ctime ) ) >= "'.date('Y-m-d',time()).'"';
        }
        if(!empty($request['end_time'])){
            $where .= ' and DATE( FROM_UNIXTIME( ctime ) ) <= "'.$request['end_time'].'"';
        }
        $list=Db::name('Supply')->where($where)->order("id DESC")->paginate(8);
        //echo Db::name('Supply')->getlastsql();
        $list->appends($request);
        $this->assign("page", $list);
        $this->assign("list", $list);
        $this->assign("uinfo", $uinfo);
        return $this->fetch();
    }
    
    public function jiaojie(){
        $param = $this->request->param();
        $uid= cmf_get_current_user_id();
        $uinfo=get_operator_data($uid);
        $today_time=strtotime('today');
        $where['operatorid']=$uid;
        $where['storeid']=$uinfo['store_id'];
        $where['payStatus']=1;
        $start=strtotime($param['start']);
        $end=strtotime($param['end']);
		session('start',$start);
        session('end',$end);
        $where['utime'] = array(array('egt',$start),array('elt',$end));
        $payType=array('1'=>'会员卡支付','2'=>'现金支付','3'=>'积分支付','4'=>'微信支付','5'=>'支付宝','6'=>'外卖支付');
        foreach($payType as $k=>$v){
            $where['payType']=$k;
            $total_money=Db::name('Order')->where($where)->sum('actualMoney');//该店铺该商品总营业额
            $list[$k]['total']=$total_money;
        }
        unset($where['payType']);
        $con['b.utime'] = array(array('egt',$start),array('elt',$end));
        $classifyTotal = Db::name("order")->where('a.paystatus=1')->where($con)->where('a.operatorid='.$uid)
        ->alias('a')->join('__ORDER_DETAIL__ b','a.orderNo=b.orderid')->join('__GOODS__ c','c.g_id=b.goodsid')
        ->join('__GOODS_CLASSIFY__ d','d.id=c.g_category')->field('a.*,d.classify_name,sum(b.money),count(b.goodsid)')->group('d.classify_name')->select()->toarray();
        
        $map['ctime'] = array(array('egt',$start),array('elt',$end));
        $map['storeid']=$uinfo['store_id'];
        $map['operatorid']=$uid;
        $supply=DB::name('supply')->where($map)->select();
        $supply_money=Db::name('supply')->where($map)->sum('money');//该店铺该商品总营业额
        $total_money=Db::name('Order')->where($where)->sum('totalMoney');//该店铺该商品总营业额
        $total_money+=$supply_money;
        $num=Db::name('Order')->where($where)->count();//订单数
        unset($where['payStatus']);
        $where['payStatus']=-3;
        $tuikuan_money=Db::name('Order')->where($where)->sum('totalMoney');//tuipin总额
        $renjun='';
        if(!empty($total_money) && !empty($num)){
            $renjun=number_format($total_money/$num);
        }
        $this->assign('tuikuan_money',$tuikuan_money);
        $this->assign('renjun',$renjun);
        $this->assign('total_money',$total_money);
        $this->assign('num',$num);
        $this->assign('start',$param['start']);
        $this->assign('end',$param['end']);
        $this->assign('supply',$supply);
        $this->assign('supply',$supply);
        $this->assign('list',$list);
        $this->assign('uinfo',$uinfo);
        return $this->fetch();
    }
    
}